package com.wolfpire.section.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.wolfpire.chapter.dao.ChapterDao;
import com.wolfpire.chapter.dao.SubjectChapterRelationDao;
import com.wolfpire.profession.dao.ProfessionDao;
import com.wolfpire.section.dao.ChapterSectionRelationDao;
import com.wolfpire.section.dao.SectionDao;
import com.wolfpire.section.dao.StudyHistoryDao;
import com.wolfpire.section.service.SectionService;
import com.wolfpire.section.service.StudyHistoryService;
import com.wolfpire.subject.dao.ProfessionSubjectRelationDao;
import com.wolfpire.subject.dao.SubjectDao;
import com.wolfpire.subject.service.SubjectService;
import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.MD5;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.IBaseHibernateDao;
import com.wolfpire.system.common.base.service.impl.BaseHibernateService;
import com.wolfpire.system.common.util.StringUtils;
import com.wolfpire.system.dao.AuthorityDao;
import com.wolfpire.system.dao.OrgDao;
import com.wolfpire.system.dao.OrgUserRelationDao;
import com.wolfpire.system.dao.RoleDao;
import com.wolfpire.system.dao.UserDao;
import com.wolfpire.system.dao.UserRoleRelationDao;
import com.wolfpire.system.model.Attachment;
import com.wolfpire.system.model.Authority;
import com.wolfpire.system.model.Chapter;
import com.wolfpire.system.model.ChapterSectionRelation;
import com.wolfpire.system.model.Org;
import com.wolfpire.system.model.OrgUserRelation;
import com.wolfpire.system.model.Profession;
import com.wolfpire.system.model.ProfessionSubjectRelation;
import com.wolfpire.system.model.Section;
import com.wolfpire.system.model.StudyHistory;
import com.wolfpire.system.model.Subject;
import com.wolfpire.system.model.SubjectChapterRelation;
import com.wolfpire.system.model.User;
import com.wolfpire.system.model.UserRoleRelation;
import com.wolfpire.system.service.AttachmentService;
import com.wolfpire.system.service.UserService;

@Service("studyHistoryService")
public class StudyHistoryServiceImpl extends BaseHibernateService<StudyHistory, Long> implements
	StudyHistoryService {
	
	@Resource private StudyHistoryDao studyHistoryDao;
	@Resource private SectionDao sectionDao;
	@Resource private ChapterSectionRelationDao chapterSectionRelationDao;
	@Resource private ChapterDao chapterDao;
	@Resource private SubjectChapterRelationDao subjectChapterRelationDao;
	@Resource private SubjectDao subjectDao;
	public List<StudyHistory> list(StudyHistory entity) {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 保存用户学习对应课程章节的学习记录
	 * @param userId
	 * @param sectionId
	 */
	public void saveStudyHistory(long userId, long sectionId, String flag, String content, String xingxing) {
		// TODO Auto-generated method stub
		Section section = this.sectionDao.get(sectionId);
		ChapterSectionRelation csr = null;
		SubjectChapterRelation scr = null;
		Chapter chapter = null;
		Subject subject = null;
		if(section != null)
			csr = this.chapterSectionRelationDao.getCSRelation(sectionId);
		if(csr != null)
			chapter = this.chapterDao.get(csr.getChapterId());
		if(chapter != null)
			scr = this.subjectChapterRelationDao.getSCRelation(chapter.getId());
		if(scr != null)
			subject = this.subjectDao.get(scr.getSubjectId());
		StudyHistory sh = new StudyHistory(userId, subject.getId(),sectionId, subject.getName(), 
				chapter.getName(), section.getName(), content, new Date(), flag, xingxing);
		this.studyHistoryDao.save(sh);
	}
	@Override
	protected IBaseHibernateDao<StudyHistory, Long> getEntityDao() {
		// TODO Auto-generated method stub
		return this.studyHistoryDao;
	}
	public List<StudyHistory> findStudyHistory(long userId, String date) {
		// TODO Auto-generated method stub
		return this.studyHistoryDao.findStudyHistory(userId, date);
	}
	public int complempercent(Long subjectId, Long userId) {
		// TODO Auto-generated method stub
		return this.studyHistoryDao.complempercent(subjectId, userId);
	}
	public List<StudyHistory> findPinLunBySection(long sectionId,
			String flag) {
		// TODO Auto-generated method stub
		return this.studyHistoryDao.findPinLunBySection(sectionId, flag);
	}
	
		
}
